﻿Imports System.Collections.Specialized
Imports System.IO
Imports System.Globalization
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Collections.ObjectModel

Class Application

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.

    ' The name of the application folder.  This folder is used to save the files 
    ' for this application such as the photos, stories and family data.
    Private Const ApplicationFolderName As String = "TFS Sticky Buddy"

    Public Const CodeplexProjectName As String = "TFSStickyBuddy"
    Public Const CodeplexReleaseId As Integer = 12624

    'The main connection to team server for the whole application
    ' The FamilyCollection and Family fields are accessed from the same thread,
    ' so suppressing the CA2211 code analysis warning.
    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Private Shared m_TFServer As TeamFoundationServer = Nothing

    Public Shared Property TFServer() As TeamFoundationServer
        Get
            Return m_TFServer
        End Get
        Set(ByVal value As TeamFoundationServer)
            m_TFServer = value
            m_TFServer.EnsureAuthenticated()
            m_WorkItemStore = DirectCast(value.GetService(GetType(WorkItemStore)), WorkItemStore)
            m_SelectedProject = Nothing
            ' Set Project to fist permissioned project
            For Each p As Project In m_WorkItemStore.Projects
                If p.HasWorkItemWriteRights Then
                    m_SelectedProject = p
                    Exit For
                End If
            Next
        End Set
    End Property

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Private Shared m_WorkItemStore As WorkItemStore = Nothing

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Public Shared ReadOnly Property WorkItemStore() As WorkItemStore
        Get
            Return m_WorkItemStore
        End Get
    End Property

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Private Shared m_SelectedProject As Project = Nothing

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Public Shared Property SelectedProject() As Project
        Get
            Return m_SelectedProject
        End Get
        Set(ByVal value As Project)
            m_SelectedProject = value
        End Set
    End Property

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Private Shared m_SelectedStoredQuery As StoredQuery

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Public Shared Property SelectedStoredQuery() As StoredQuery
        Get
            Return m_SelectedStoredQuery
        End Get
        Set(ByVal value As StoredQuery)
            m_SelectedStoredQuery = value
        End Set
    End Property

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
    Private Shared m_DisplayBy As Node.TreeType = Node.TreeType.Area

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
Public Shared Property DisplayBy() As Node.TreeType
        Get
            Return m_DisplayBy
        End Get
        Set(ByVal value As Node.TreeType)
            m_DisplayBy = value
        End Set
    End Property


    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
Public Shared ItemWrappers As New ItemCollection

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")> _
Public Shared WorkItems As New ObservableCollection(Of WorkItem)

    Friend Shared Sub PopulateResources(ByVal Skin As String, ByVal LifeCycle As String)
        ' Create new Resource
        Dim rd As New ResourceDictionary

        ' Add skinn bit
        Dim SkinBit As Boolean = False
        If Not String.IsNullOrEmpty(Skin) Then
            Try
                ' Add Skin bit
                Dim uri As New Uri(Skin, UriKind.Relative)
                Dim Component As ResourceDictionary = CType(Application.LoadComponent(uri), ResourceDictionary)
                rd.MergedDictionaries.Add(Component)
                SkinBit = True
            Catch ex As Exception

            End Try
        End If
        ' Add default if not there
        If Not SkinBit Then
            Dim uri As New Uri("Resources\Skins\Black\BlackResources.xaml", UriKind.Relative)
            Dim Component As ResourceDictionary = CType(Application.LoadComponent(uri), ResourceDictionary)
            rd.MergedDictionaries.Add(Component)
        End If

        ' Add LifeCycle bit
        Dim LifeCycleBit As Boolean = False
        If Not String.IsNullOrEmpty(LifeCycle) Then
            Try
                Dim uri As New Uri(LifeCycle, UriKind.Relative)
                Dim Component As ResourceDictionary = CType(Application.LoadComponent(uri), ResourceDictionary)
                rd.MergedDictionaries.Add(Component)
                LifeCycleBit = True
            Catch ex As Exception

            End Try
        End If
        ' Add default if not there
        If Not LifeCycleBit Then
            Dim uri As New Uri("Resources\LifeCycles\Default\DefaultResources.xaml", UriKind.Relative)
            Dim Component As ResourceDictionary = CType(Application.LoadComponent(uri), ResourceDictionary)
            rd.MergedDictionaries.Add(Component)
        End If

        ' Add resources
        Application.Current.Resources = rd
    End Sub

    ''' <summary>
    ''' Occurs when the application starts.
    ''' </summary>
    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")> _
    Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)
        Dim appSettings As Settings = Settings.Default

        PopulateResources(appSettings.Skin, appSettings.LifeCycle)
        
        MyBase.OnStartup(e)
    End Sub


    ''' <summary>
    ''' Occurs when the application exits.
    ''' </summary>
    Protected Overrides Sub OnExit(ByVal e As ExitEventArgs)
        ' Save the file automatically
        'If (Family.IsDirty & Not String.IsNullOrEmpty(FamilyCollection.FullyQualifiedFilename)) Then
        '    FamilyCollection.Save()
        'End If
        MyBase.OnExit(e)
    End Sub

    ''' <summary>
    ''' Gets the collection of skins
    ''' </summary>
    Public Shared ReadOnly Property Skins() As NameValueCollection
        Get
            Dim skinlist As NameValueCollection = New NameValueCollection()
            Dim SkinFolder As String = My.Resources.Skins
            For Each folder As String In Directory.GetDirectories(SkinFolder)
                For Each file As String In Directory.GetFiles(folder)
                    Dim fileInfo As FileInfo = New FileInfo(file)
                    If String.Compare(fileInfo.Extension, My.Resources.XamlExtension, True, CultureInfo.InvariantCulture) = 0 Then
                        ' Use the first part of the resource file name for the menu item name.
                        skinlist.Add(fileInfo.Name.Remove(fileInfo.Name.IndexOf(My.Resources.ResourcesString)), Path.Combine(folder, fileInfo.Name))
                    End If
                Next
            Next
            Return skinlist
        End Get
    End Property

    ''' <summary>
    ''' Gets the collection of LifeCycles
    ''' </summary>
    Public Shared ReadOnly Property LifeCycles() As NameValueCollection
        Get
            Dim LifeCyclelist As NameValueCollection = New NameValueCollection()
            Dim LifeCycleFolder As String = My.Resources.LifeCycles
            For Each folder As String In Directory.GetDirectories(LifeCycleFolder)
                For Each file As String In Directory.GetFiles(folder)
                    Dim fileInfo As FileInfo = New FileInfo(file)
                    If String.Compare(fileInfo.Extension, My.Resources.XamlExtension, True, CultureInfo.InvariantCulture) = 0 Then
                        ' Use the first part of the resource file name for the menu item name.
                        LifeCyclelist.Add(fileInfo.Name.Remove(fileInfo.Name.IndexOf(My.Resources.ResourcesString)), Path.Combine(folder, fileInfo.Name))
                    End If
                Next
            Next
            Return LifeCyclelist
        End Get
    End Property

    ''' <summary>
    ''' Return the animation duration. The duration is extended
    ''' if special keys are currently pressed (for demo purposes)  
    ''' otherwise the specified duration is returned. 
    ''' </summary>
    Public Shared Function GetAnimationDuration(ByVal milliseconds As Double) As TimeSpan
        If Keyboard.IsKeyDown(Key.F12) Then
            Return TimeSpan.FromMilliseconds(milliseconds * 5)
        Else
            Return TimeSpan.FromMilliseconds(milliseconds)
        End If
    End Function



End Class
